package com.yuan.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuan.ssm.entity.Power;
import com.yuan.ssm.service.PowerService;
import com.yuan.ssm.util.SNSResultUtil;
import com.yuan.ssm.vo.Page;
import com.yuan.ssm.vo.SNSResult;

@RestController
@RequestMapping("sysUser")
public class SysUserController {
	@Autowired
	private PowerService powerService;

	@GetMapping("listPage")
	public Page<Power> query(@RequestParam(name = "codeNumber", required = false) String codeNumber,
			@RequestParam(name = "operatingAuthorization", required = false) String operatingAuthorization,
			@RequestParam("page") Integer page, @RequestParam("rows") Integer rows) {
		return powerService.listAll(codeNumber, operatingAuthorization, page, rows);
	}
	
	@PutMapping
	public SNSResult<?> update(@RequestBody Power user) {
		try {
			powerService.update(user);
			return SNSResultUtil.getSuccessResult();
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
	@DeleteMapping
	public SNSResult<?> delete(@RequestParam("id") Long id) {
		if (powerService.delete(id)) {
			return SNSResultUtil.getSuccessResult();
		} 
		return SNSResultUtil.getErrorResult("删除失败");
	}
	@PostMapping
	public SNSResult<?> add(@RequestBody Power user) {
		try {
			powerService.add(user);
			return SNSResultUtil.getSuccessResult();
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
	@GetMapping
	public SNSResult<?> getByName(@RequestParam("codeNumber") String codeNumber) {
		try {
			return SNSResultUtil.getSuccessResult(powerService.getByName(codeNumber));
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
	@GetMapping("listAll")
	public SNSResult<?> listAll() {
		try {
			return SNSResultUtil.getSuccessResult(powerService.listAll());
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
}
